//
//  DatabaseTool.m
//  豆瓣
//
//  Created by dllo on 16/3/29.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "DatabaseTool.h"

@implementation DatabaseTool

+(instancetype)shareDatabaseTool{
    static DatabaseTool *tool = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        tool = [[DatabaseTool alloc]init];
        [tool creatDatabase];
        [tool creatTable];
    });
    return tool;
}

-(void)creatDatabase{
    NSString  *sandbox = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES) lastObject];
    NSString *path = [sandbox stringByAppendingString:@"activity.sqlite"];
    NSLog(@"%@",path);
    int result =sqlite3_open([path UTF8String], &sqlite);
    if (result == SQLITE_OK) {
        NSLog(@"打开数据成功");
    }else{
        NSLog(@"失败:%d",result);
   }
}
- (void)creatTable{
    NSString *sqlStr = @"create table if not exists reading (Id text primary key, title text)";
    int result = sqlite3_exec(sqlite, [sqlStr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }else{
        NSLog(@"创建失败:%d",result);
    }
    
}
-(int)inserReading:(CWCDataModel *)read{
    NSLog(@"%@",read);
    NSString *sqlstr = [NSString stringWithFormat:@"insert into reading(Id, title) values ('%@', '%@')", read.docid,read.title];
    int result = sqlite3_exec(sqlite, [sqlstr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"添加成功");
    }else{
        NSLog(@"添加失败:%d",result);
        
    }
    return result;
}
-(int)deleteReading:(CWCDataModel *)read{
    NSString *sqlstr = [NSString stringWithFormat: @"delete from reading where title = '%@'",read.title];
    int result = sqlite3_exec(sqlite, [sqlstr UTF8String], nil, nil, nil);
    if (result == SQLITE_OK) {
        NSLog(@"删除活动成功");
    }else {
        NSLog(@"删除活动失败:%d",result);
    }
    return result;
}

- (NSMutableArray *)selectAllreading{
    NSString *sqlstr = [NSString stringWithFormat:@"select * from reading"];
    sqlite3_stmt * stmt = nil;
    int result = sqlite3_prepare_v2(sqlite, [sqlstr UTF8String], -1, &stmt, nil);
    NSMutableArray * arr = [NSMutableArray array];
    if (result == SQLITE_OK) {
        NSLog(@"查询成功");
        while (sqlite3_step(stmt) == SQLITE_ROW) {
            //int number = sqlite3_column_int(stmt, 0);
            const unsigned char *title = sqlite3_column_text(stmt, 1);
            NSString *readingTitle = [NSString stringWithUTF8String:(const char *)title];
            const unsigned char *id = sqlite3_column_text(stmt, 0);
            NSString *readingId = [NSString stringWithUTF8String:(const char *)id];
            NSLog(@"1212%@",readingId);
            NSLog(@"1212%@",readingTitle);
            CWCDataModel *read = [[CWCDataModel alloc]init];
            read.title = readingTitle;
            read.docid = readingId;
            [arr addObject:read];
        }
        return arr;
        
    }else{
        NSLog(@"查询错误:%d",result);
        return nil;
    }
}









@end
